﻿using System.Collections.Generic;

namespace AladdinGameFramework.Fsm
{
	/// <summary>
	/// 有限状态机管理器。
	/// </summary>
	internal sealed class FsmManager : GameFrameworkModule, IFsmManager
	{
		private readonly Dictionary<string, FsmBase> m_Fsms;
		private readonly List<FsmBase> m_TempFsms;

		/// <summary>
		/// 初始化有限状态机管理器的新实例。
		/// </summary>
		public FsmManager()
		{
			m_Fsms = new Dictionary<string, FsmBase>();
			m_TempFsms = new List<FsmBase>();
		}

		/// <summary>
		/// 获取游戏框架模块优先级。
		/// </summary>
		/// <remarks>优先级较高的模块会优先轮询，并且关闭操作会后进行。</remarks>
		internal override int Priority
		{
			get
			{
				return 60;
			}
		}

		/// <summary>
		/// 获取有限状态机数量。
		/// </summary>
		public int Count
		{
			get
			{
				return m_Fsms.Count;
			}
		}

		/// <summary>
		/// 有限状态机管理器轮询。
		/// </summary>
		/// <param name="elapseSeconds">逻辑流逝时间，以秒为单位。</param>
		/// <param name="realElapseSeconds">真实流逝时间，以秒为单位。</param>
		internal override void Update(float elapseSeconds, float realElapseSeconds)
		{
			m_TempFsms.Clear();
			if (m_Fsms.Count <= 0)
			{
				return;
			}

			foreach (KeyValuePair<string, FsmBase> fsm in m_Fsms)
			{
				m_TempFsms.Add(fsm.Value);
			}

			foreach (FsmBase fsm in m_TempFsms)
			{
				if (fsm.IsDestroyed)
				{
					continue;
				}

				fsm.Update(elapseSeconds, realElapseSeconds);
			}
		}

		/// <summary>
		/// 关闭并清理有限状态机管理器。
		/// </summary>
		internal override void Shutdown()
		{
			foreach (KeyValuePair<string, FsmBase> fsm in m_Fsms)
			{
				fsm.Value.Shutdown();
			}

			m_Fsms.Clear();
			m_TempFsms.Clear();
		}

		/// <summary>
		/// 检查是否存在有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <returns>是否存在有限状态机。</returns>
		public bool HasFsm<T>() where T : class
		{
			return HasFsm<T>(string.Empty);
		}

		/// <summary>
		/// 检查是否存在有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <param name="name">有限状态机名称。</param>
		/// <returns>是否存在有限状态机。</returns>
		public bool HasFsm<T>(string name) where T : class
		{
			return m_Fsms.ContainsKey(Utility.Text.GetFullName<T>(name));
		}

		/// <summary>
		/// 获取有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <returns>要获取的有限状态机。</returns>
		public IFsm<T> GetFsm<T>() where T : class
		{
			return GetFsm<T>(string.Empty);
		}

		/// <summary>
		/// 获取有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <param name="name">有限状态机名称。</param>
		/// <returns>要获取的有限状态机。</returns>
		public IFsm<T> GetFsm<T>(string name) where T : class
		{
			FsmBase fsm = null;
			if (m_Fsms.TryGetValue(Utility.Text.GetFullName<T>(name), out fsm))
			{
				return (IFsm<T>)fsm;
			}

			return null;
		}

		/// <summary>
		/// 获取所有有限状态机。
		/// </summary>
		/// <returns>所有有限状态机。</returns>
		public FsmBase[] GetAllFsms()
		{
			int index = 0;
			FsmBase[] fsms = new FsmBase[m_Fsms.Count];
			foreach (KeyValuePair<string, FsmBase> fsm in m_Fsms)
			{
				fsms[index++] = fsm.Value;
			}

			return fsms;
		}

		/// <summary>
		/// 创建有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <param name="owner">有限状态机持有者。</param>
		/// <param name="states">有限状态机状态集合。</param>
		/// <returns>要创建的有限状态机。</returns>
		public IFsm<T> CreateFsm<T>(T owner, params FsmState<T>[] states) where T : class
		{
			return CreateFsm(string.Empty, owner, states);
		}

		/// <summary>
		/// 创建有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <param name="name">有限状态机名称。</param>
		/// <param name="owner">有限状态机持有者。</param>
		/// <param name="states">有限状态机状态集合。</param>
		/// <returns>要创建的有限状态机。</returns>
		public IFsm<T> CreateFsm<T>(string name, T owner, params FsmState<T>[] states) where T : class
		{
			if (HasFsm<T>(name))
			{
				throw new GameFrameworkException(string.Format("Already exist FSM '{0}'.", Utility.Text.GetFullName<T>(name)));
			}

			Fsm<T> fsm = new Fsm<T>(name, owner, states);
			m_Fsms.Add(Utility.Text.GetFullName<T>(name), fsm);
			return fsm;
		}

		/// <summary>
		/// 销毁有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <returns>是否销毁有限状态机成功。</returns>
		public bool DestroyFsm<T>() where T : class
		{
			return DestroyFsm<T>(string.Empty);
		}

		/// <summary>
		/// 销毁有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <param name="name">要销毁的有限状态机名称。</param>
		/// <returns>是否销毁有限状态机成功。</returns>
		public bool DestroyFsm<T>(string name) where T : class
		{
			string fullName = Utility.Text.GetFullName<T>(name);
			FsmBase fsm = null;
			if (m_Fsms.TryGetValue(fullName, out fsm))
			{
				fsm.Shutdown();
				return m_Fsms.Remove(fullName);
			}

			return false;
		}

		/// <summary>
		/// 销毁有限状态机。
		/// </summary>
		/// <typeparam name="T">有限状态机持有者类型。</typeparam>
		/// <param name="fsm">要销毁的有限状态机。</param>
		/// <returns>是否销毁有限状态机成功。</returns>
		public bool DestroyFsm<T>(IFsm<T> fsm) where T : class
		{
			if (fsm == null)
			{
				throw new GameFrameworkException("FSM is invalid.");
			}

			return DestroyFsm<T>(fsm.Name);
		}
	}
}
